<?php

require_once dirname(__FILE__) . '/../lib/ct_libroGeneratorConfiguration.class.php';
require_once dirname(__FILE__) . '/../lib/ct_libroGeneratorHelper.class.php';

/**
 * ct_libro actions.
 *
 * @package    ingenyo-biblio
 * @subpackage ct_libro
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class ct_libroActions extends autoCt_libroActions
{

  public function executeNew(sfWebRequest $request)
  {
    $documento = $this->getUser()->getAttribute('documento');
    $libro = new CtLibro();
    $libro->setIddocumento($documento->getId());
    $this->form = new CtlibroForm($libro);
    $this->CtLibro = $this->form->getObject();
    $this->CtDocumento = $documento;

    $this->setLayout('layout_iframe');

  }

  public function executeEdit(sfWebRequest $request)
  {
    parent::executeEdit($request);
    $this->setLayout('layout_iframe');

  }

  public function executeIndex(sfWebRequest $request)
  {
    parent::executeIndex($request);
    $this->setLayout('layout_iframe');

  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->form = $this->configuration->getForm();
    $this->CtLibro = $this->form->getObject();
    $this->CtDocumento = $this->getUser()->getAttribute('documento');

    $this->processForm($request, $this->form);

    $this->setLayout('layout_iframe');

  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $notice = $form->getObject()->isNew() ? 'The item was created successfully.' : 'The item was updated successfully.';

      $CtLibro = $form->save();

      $this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $CtLibro)));

      if ($request->hasParameter('_save_and_add'))
      {
        $this->getUser()->setFlash('notice', $notice . ' You can add another one below.');

        $this->redirect('@ct_libro_new');
        $this->setLayout('layout_iframe');
      }
      else
      {
        $this->getUser()->setFlash('notice', $notice);

        $this->setLayout('layout_iframe');
        $this->setTemplate('confirm');
      }
    }
    else
    {
      $this->getUser()->setFlash('error', 'The item has not been saved due to some errors.', false);
      $this->setLayout('layout_iframe');
      $this->setTemplate('new');
    }

  }

  protected function buildQuery()
  {
    $query = PropelQuery::from('CtLibro');

    foreach ($this->configuration->getWiths() as $with)
    {
      $query->joinWith($with);
    }

    foreach ($this->configuration->getQueryMethods() as $method)
    {
      $query->$method($this->getUser()->getAttribute('documento'));
    }

    $this->processSort($query);

    $event = $this->dispatcher->filter(new sfEvent($this, 'admin.build_criteria'), $query);
    $query = $event->getReturnValue();

    return $query;

  }

  public function executeBatchActivar(sfWebRequest $request)
  {
    $ids = $request->getParameter('ids');

    foreach (CtLibroPeer::retrieveByPks($ids) as $libro)
    {
      $libro->setEstadoActivo();
      $libro->save();
    }

    $this->getUser()->setFlash('notice', 'Activacion exitosa');
    $this->redirect('@ct_libro');

  }

}
